<template>
  <div class="table-box">
    <div class="table-search">
      <el-form :inline="true" :model="form">
        <el-form-item label="审核组名称">
          <el-input clearable v-model="form.name" placeholder="审核组名称" />
        </el-form-item>
        <el-form-item label="审核规则">
          <el-select v-model="form.rule_id" clearable>
            <el-option
              v-for="item in rulesList"
              :key="item.rule_id"
              :label="item.name"
              :value="item.rule_id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="订单类型">
          <el-select v-model="form.source_type" clearable>
            <el-option
              v-for="item in sourceTypeList"
              :key="item.value"
              :label="item.name"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onQuery">查询</el-button>
          <el-button type="danger" @click="onAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="card content-box">
      <div class="table">
        <el-table :data="tableData" border v-loading="loading">
          <el-table-column prop="id" label="ID" align="center" />
          <el-table-column prop="name" label="组名称" align="center" />
          <el-table-column prop="rule_name" label="审核规则" align="center" />
          <el-table-column
            prop="source_type_name"
            label="订单类型"
            align="center"
          />
          <!--<el-table-column prop="audit_user" label="审核人" align="center" />-->
          <el-table-column prop="audit_level" label="审核层级" align="center" />
          <el-table-column prop="level_name" label="层级说明" align="center" />
          <el-table-column label="是否终审" align="center">
            <template #default="scope">
              <el-tag :type="scope.row.is_finally ? 'success' : 'warning'">
                {{ scope.row.is_finally ? "是" : "否" }}</el-tag
              >
            </template>
          </el-table-column>
          <el-table-column
            prop="audit_params"
            label="其他参数"
            align="center"
          />
          <el-table-column prop="remark" label="备注" align="center" />
          <el-table-column
            label="操作"
            align="center"
            fixed="right"
            width="100"
          >
            <template #default="scope">
              <el-button
                type="warning"
                @click="handleOperate(scope.$index, scope.row, 'edit')"
                >编辑</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <div class="table-pagination">
          <el-pagination
            :current-page="pageable.pageNum"
            :page-size="pageable.pageSize"
            :page-sizes="[10, 25, 50, 100]"
            :background="true"
            layout="total, sizes, prev, pager, next, jumper"
            :total="pageable.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          ></el-pagination>
        </div>
      </div>
    </div>
    <DialogForm
      @cancel="cancel"
      @closeDialog="closeDialog"
      @submitForm="submit"
      :dialogFormVisible="dialogVisible"
      :dialogForm="dialogForm"
      :btnLoading="btnLoading"
      :dialogTitle="dialogTitle"
      :rulesList="rulesList"
    />
  </div>
</template>

<script setup>
import { ref, onMounted, reactive } from "vue";
import {
  reviewGroupsApi,
  reviewGroupsUpdateApi,
} from "@/api/ExAssistant/review";
import DialogForm from "./components/dialogForm.vue";

const loading = ref(false);
const tableData = ref([]);
const dialogVisible = ref(false);
const dialogTitle = ref("");
const btnLoading = ref(false);
const btnType = ref(null);
const sourceTypeList = ref([]);
const rulesList = ref([]);
const formData = {
  name: "",
  source_type: "",
  rule_id: "",
};
const form = reactive({ ...formData });
const dialogFormData = {
  id: "",
  name: "",
  rule_id: "",
  audit_level: "",
  level_name: "",
  audit_params: "",
  is_finally: "",
  remark: "",
};
const dialogForm = reactive({ ...dialogFormData });
const pageable = reactive({
  pageNum: 1,
  pageSize: 10,
  total: 0,
});

//参数
const param = () => {
  return {
    page: pageable.pageNum,
    pagesize: pageable.pageSize,
    name: form.name,
    rule_id: form.rule_id,
    source_type: form.source_type,
  };
};

// 获取表格数据
const getTbaleData = async () => {
  loading.value = true;

  try {
    const res = await reviewGroupsApi(param());
    loading.value = false;
    tableData.value = res.data.results;
    pageable.total = res.data.count;
    sourceTypeList.value = res.data.extra.source_type_list;
    rulesList.value = res.data.extra.rules_list;
  } catch (error) {
    loading.value = false;
  }
};
//查询
const onQuery = async () => {
  getTbaleData();
};
//新增
const onAdd = () => {
  dialogTitle.value = "新增审核组";
  dialogVisible.value = true;
  Object.assign(dialogForm, { ...dialogFormData });
};
// 行操作
const handleOperate = async (index, row, type) => {
  // 编辑
  if (type === "edit") {
    btnType.value = "edit";
    dialogTitle.value = "编辑审核组";
    dialogVisible.value = true;
    Object.assign(dialogForm, { ...row });
  }
};
// 提交
const submit = async (param) => {
  try {
    btnLoading.value = true;
    let res;
    res = await reviewGroupsUpdateApi(param);
    ElMessage({
      message: res.message,
      type: "success",
    });
    dialogVisible.value = false;
    btnLoading.value = false;
    getTbaleData();
  } catch (error) {
    btnLoading.value = false;
    dialogVisible.value = false;
  }
};
//关闭弹窗
const closeDialog = () => {
  dialogVisible.value = false;
  btnType.value = null;
  Object.assign(dialogForm, { ...dialogFormData });
};

// 取消
const cancel = () => {
  dialogVisible.value = false;
  btnType.value = null;
  Object.assign(dialogForm, { ...dialogFormData });
};

// 条数
const handleSizeChange = (number) => {
  pageable.pageSize = number;
  getTbaleData();
};

// 页码
const handleCurrentChange = (number) => {
  pageable.pageNum = number;
  getTbaleData();
};
onMounted(() => {
  getTbaleData();
});
</script>
